package com.component.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.component.domain.MMenu;
import com.component.service.MMenuService;
import com.component.mapper.MMenuMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 *
 */
@CacheConfig(cacheNames = "MMenuServiceImpl")
@Service
public class MMenuServiceImpl extends ServiceImpl<MMenuMapper, MMenu>
    implements MMenuService{

    @Cacheable(cacheNames = "getById",key = "#id")
    @Override
    public MMenu getById(Serializable id) {
        return super.getById(id);
    }

    @Cacheable(cacheNames = "parentList")
    @Override
    public List<MMenu> parentList() {
        LambdaQueryWrapper<MMenu> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MMenu::getPid,0);
        lambdaQueryWrapper.eq(MMenu::getMenuLevel,1);
        lambdaQueryWrapper.orderByAsc(MMenu::getCreatetime);
        List<MMenu> mMenuList=this.baseMapper.selectList(lambdaQueryWrapper);
        return mMenuList;
    }

    @Cacheable(cacheNames = "childrenList",key = "#pid")
    @Override
    public List<MMenu> childrenList(String pid) {
        LambdaQueryWrapper<MMenu> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MMenu::getPid,pid);
        lambdaQueryWrapper.eq(MMenu::getMenuLevel,2);
        lambdaQueryWrapper.orderByAsc(MMenu::getCreatetime);
        List<MMenu> mMenuList=this.baseMapper.selectList(lambdaQueryWrapper);
        return mMenuList;
    }

    @Override
    public List<MMenu> getMMenuList(List<Integer> mids) {
        LambdaQueryWrapper<MMenu> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(MMenu::getId,mids);
        List<MMenu> menuList=this.baseMapper.selectList(lambdaQueryWrapper);
        return menuList;
    }

    @Override
    public IPage<MMenu> pageChild(Integer pageNum,Integer pageSize,String menuId,String menuName,String componentName,String path) {
        LambdaQueryWrapper<MMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(menuId)){
            lambdaQueryWrapper.eq(MMenu::getPid,menuId);
        }
        if(StringUtils.isNotBlank(menuName)){
            lambdaQueryWrapper.like(MMenu::getMenuName,menuName);
        }
        if (StringUtils.isNotBlank(componentName)) {
            lambdaQueryWrapper.like(MMenu::getMenuComponent,componentName);
        }
        if(StringUtils.isNotBlank(path)){
            lambdaQueryWrapper.like(MMenu::getMenuPath,path);
        }
        lambdaQueryWrapper.eq(MMenu::getMenuLevel,2);
        lambdaQueryWrapper.orderByAsc(MMenu::getCreatetime);
        IPage<MMenu> iPage=this.baseMapper.selectPage(new Page(pageNum,pageSize),lambdaQueryWrapper);
        return iPage;
    }

}




